package com.westcom.vulb.network.http;


import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.Header;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MinimalField;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.westcom.vulb.logic.SessionManager;
import com.westcom.vulb.protocal.http.Request;
import com.westcom.vulb.util.MyLog;


public class SyncPost {
	private DefaultHttpClient mHttpClient;
	
    public static SyncPost create() {
        return new SyncPost();
    }
    
    public SyncPost() {
	    mHttpClient = new DefaultHttpClient();
    }
    public String postJson(String url,org.apache.http.Header[] h,HttpEntity entity) throws ClientProtocolException, IOException
    {
    	HttpPost httppost = new HttpPost(url);
    	httppost.setHeaders(h);
        HttpResponse httpResponse = null;
        httppost.setEntity(entity);
       // mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
        httpResponse =  mHttpClient.execute(httppost);
        HttpEntity r_entity = httpResponse.getEntity();
        String responseString = EntityUtils.toString(r_entity);
        return responseString;
    }
    
    public String postJson2(String url,String param) throws ClientProtocolException, IOException
    {
        String fullUrl = Request.HOST + url;
        HttpPost httppost = new HttpPost(fullUrl);
        
        MyLog.d("Request url: " + url + " param:" + param);
        
        HttpResponse httpResponse = null;
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
        multipartEntity.addPart("params", new StringBody(param));
        
        //TODO improve design
        if (url != Request.URL_LOGIN) {
            httppost.addHeader("vssid", SessionManager.getInstance().getCurrSessionId());
        }

        httppost.setEntity(multipartEntity);

       // mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
        httpResponse =  mHttpClient.execute(httppost);
        HttpEntity r_entity = httpResponse.getEntity();
        String responseString = EntityUtils.toString(r_entity);
        String str = responseString;
        
        MyLog.d("Response: " + responseString);
        return responseString;
    } 
}